package com.go.where.family.component.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;

import com.go.where.family.component.MessageComponent;
import com.go.where.family.constants.Constant;
import com.go.where.family.constants.TransmissionCode;
import com.go.where.family.exception.ServerDisponseException;
import com.go.where.family.pojo.CustomerCommentInfo;
import com.go.where.family.pojo.FamilySquareInfo;
import com.go.where.family.pojo.MessageInfo;
import com.go.where.family.pojo.MsgCustomerRelation;
import com.go.where.family.scoket.Head;
import com.go.where.family.service.CustomerCommentService;
import com.go.where.family.service.FamilySquareService;
import com.go.where.family.service.MessageService;
import com.go.where.family.utils.CommonUtil;
import com.go.where.family.vo.CustomerCommentVo;
import com.go.where.family.vo.FamilySquareVo;
import com.go.where.family.vo.MessageVo;
import com.google.gson.Gson;

@Component("messageComponent")
public class MessageComponentImpl implements MessageComponent {

	private static final Logger log = Logger.getLogger(MessageComponentImpl.class);
	private Gson gson;
	@Resource(name = "messageService")
	private MessageService messageService;
	@Resource(name = "familySquareService")
	private FamilySquareService familySquareService;
	@Resource(name = "customerCommentService")
	private CustomerCommentService customerCommentService;

	public MessageComponentImpl() {
		gson = CommonUtil.gson();
	}

	@Override
	public List<MessageInfo> querySystemList(String body, Head head) {
		MessageVo vo = gson.fromJson(body, MessageVo.class);
		if (vo != null) {
			Integer currentPage = vo.getPageIndex();
			Integer pageSize = vo.getPageCount();
			Integer pageStart = 1;
			Integer pageEnd = 10;
			if (currentPage != null && pageSize != null) {
				pageStart = (currentPage - 1) * pageSize + 1;
				pageEnd = currentPage * pageSize;
			}
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("pageStart", pageStart);
			map.put("pageEnd", pageEnd);
			map.put("isDelete", Constant.IS_NOT_DELETE);
			map.put("customerId", vo.getCustomerId());

			List<MessageInfo> list = messageService.queryList(map);
			if (list != null && !list.isEmpty()) {
				return list;
			} else {
				return new ArrayList<MessageInfo>();
			}
		} else {
			log.info("requestId:" + head.getRequestId() + ",获取系统消息列表信息时，报文解析异常。");
			throw new ServerDisponseException(TransmissionCode.ERROR_ADJUST.getCode(),
					TransmissionCode.ERROR_ADJUST.getMessage());
		}
	}

	@Override
	public MessageInfo querySystemDetail(String body, Head head) {
		MessageVo vo = gson.fromJson(body, MessageVo.class);
		if (vo != null) {
			String messageId = vo.getMessageId();
			MessageInfo mi = messageService.queryMessageDetail(messageId);
			if (mi != null) {
				return mi;
			} else {
				log.info("requestId:" + head.getRequestId() + ",查询系统消息详情信息时,没有查询到数据,课题ID：" + messageId);
				throw new ServerDisponseException(TransmissionCode.NO_DATA.getCode(),
						TransmissionCode.NO_DATA.getMessage());
			}
		} else {
			log.info("requestId:" + head.getRequestId() + ",查询系统消息详情信息时，报文解析异常");
			throw new ServerDisponseException(TransmissionCode.ERROR_ADJUST.getCode(),
					TransmissionCode.ERROR_ADJUST.getMessage());
		}
	}

	@Override
	public List<FamilySquareInfo> querySendMessageOfMine(String body, Head head) {
		FamilySquareVo vo = gson.fromJson(body, FamilySquareVo.class);
		if (vo != null) {
			Integer currentPage = vo.getPageIndex();
			Integer pageSize = vo.getPageCount();
			Integer pageStart = 1;
			Integer pageEnd = 10;
			if (currentPage != null && pageSize != null) {
				pageStart = (currentPage - 1) * pageSize + 1;
				pageEnd = currentPage * pageSize;
			}
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("pageStart", pageStart);
			map.put("pageEnd", pageEnd);
			map.put("isDelete", Constant.IS_NOT_DELETE);
			map.put("customerId", vo.getCustomerId());
			if (StringUtils.isNotBlank(vo.getOrderBy())) {
				if (Constant.ORDER_TYPE_ASC.equals(vo.getOrderType())) {
					map.put("page_orderByAsc", vo.getOrderBy());
				} else {
					map.put("page_orderByDesc", vo.getOrderBy());
				}
			}

			List<FamilySquareInfo> list = familySquareService.queryList(map);
			if (list != null && !list.isEmpty()) {
				for (FamilySquareInfo topic : list) {
					if (StringUtils.isNotBlank(topic.getHeaderPath())) {
						topic.setHeaderPath(CommonUtil.getUrlPrefix() + topic.getHeaderPath());
					}
				}
				return list;
			} else {
				return new ArrayList<FamilySquareInfo>();
			}
		} else {
			log.info("requestId:" + head.getRequestId() + ",获取我发送的消息列表信息时，报文解析异常。");
			throw new ServerDisponseException(TransmissionCode.ERROR_ADJUST.getCode(),
					TransmissionCode.ERROR_ADJUST.getMessage());
		}
	}

	@Override
	public List<CustomerCommentInfo> queryReceivedMessageOfMine(String body, Head head) {
		CustomerCommentVo vo = gson.fromJson(body, CustomerCommentVo.class);
		if (vo != null) {
			Integer currentPage = vo.getPageIndex();
			Integer pageSize = vo.getPageCount();
			Integer pageStart = 1;
			Integer pageEnd = 10;
			if (currentPage != null && pageSize != null) {
				pageStart = (currentPage - 1) * pageSize + 1;
				pageEnd = currentPage * pageSize;
			}
			Map<String, Object> paraMap = new HashMap<String, Object>();
			paraMap.put("pageStart", pageStart);
			paraMap.put("pageEnd", pageEnd);
			paraMap.put("isDelete", Constant.IS_NOT_DELETE);
			paraMap.put("targetCustomer", vo.getCustomerId());
			paraMap.put("commentType", "6");

			List<CustomerCommentInfo> list = customerCommentService.queryList(paraMap);
			if (list != null) {
				return list;
			} else {
				return new ArrayList<CustomerCommentInfo>();
			}
		} else {
			log.info("requestId:" + head.getRequestId() + ",获取我收到的消息列表时，参数报文解析异常");
			throw new ServerDisponseException(TransmissionCode.ERROR_ADJUST.getCode(),
					TransmissionCode.ERROR_ADJUST.getMessage());
		}

	}

	@Override
	public boolean delete(String body, Head head) {
		MessageVo vo = gson.fromJson(body, MessageVo.class);
		if (vo != null) {
			String id = vo.getId();
			int result = 0;
			if (Constant.SYSTEM_MESSAGE.equals(vo.getMessageType())) {
				MsgCustomerRelation rela = new MsgCustomerRelation();
				rela.setMessageId(vo.getId());
				rela.setCustomerId(vo.getCustomerId());
				result =messageService.deleteMessageRelation(rela);
			} else if (Constant.RECEIVED_MESSAGE.equals(vo.getMessageType())) {
				CustomerCommentInfo comment = new CustomerCommentInfo();
				comment.setId(id);
				comment.setIsDelete(Constant.IS_DELETE);
				result =customerCommentService.update(comment);
			} else if (Constant.SENDED_MESSAGE.equals(vo.getMessageType())) {
				FamilySquareInfo square = new FamilySquareInfo();
				square.setId(id);
				square.setIsDelete(Constant.IS_DELETE);
				result =familySquareService.update(square);
			}
			if(result>0){
				return true;
			}else{
				return false;
			}
		} else {
			log.info("requestId:" + head.getRequestId() + ",删除消息时，参数报文解析异常");
			throw new ServerDisponseException(TransmissionCode.ERROR_ADJUST.getCode(),
					TransmissionCode.ERROR_ADJUST.getMessage());
		}
	}

	@Override
	public boolean deleteAll(String body, Head head) {
		MessageVo vo = gson.fromJson(body, MessageVo.class);
		if (vo != null) {
			String customerId = vo.getCustomerId();
			if (Constant.SYSTEM_MESSAGE.equals(vo.getMessageType())) {
				MsgCustomerRelation rela = new MsgCustomerRelation();
				rela.setCustomerId(vo.getCustomerId());
				messageService.deleteMessageRelation(rela);
				return true;
			} else if (Constant.RECEIVED_MESSAGE.equals(vo.getMessageType())) {
				CustomerCommentInfo comment = new CustomerCommentInfo();
				comment.setCustomerId(customerId);
				comment.setIsDelete(Constant.IS_DELETE);
				customerCommentService.update(comment);
				return true;
			} else if (Constant.SENDED_MESSAGE.equals(vo.getMessageType())) {
				FamilySquareInfo square = new FamilySquareInfo();
				square.setCustomerId(customerId);
				square.setIsDelete(Constant.IS_DELETE);
				familySquareService.update(square);
				return true;
			}else{
				return false;
			}
		} else {
			log.info("requestId:" + head.getRequestId() + ",删除全部消息时，参数报文解析异常");
			throw new ServerDisponseException(TransmissionCode.ERROR_ADJUST.getCode(),
					TransmissionCode.ERROR_ADJUST.getMessage());
		}
	}

}
